package com.yc.videocache.sourcestorage;

import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.rdb.RawRdbPredicates;
import ohos.data.rdb.RdbOpenCallback;
import ohos.data.rdb.RdbStore;
import ohos.data.rdb.StoreConfig;
import ohos.data.rdb.ValuesBucket;

import com.yc.videocache.SourceInfo;

import java.sql.ResultSet;
import java.util.ArrayList;

import static com.yc.videocache.Preconditions.checkAllNotNull;
import static com.yc.videocache.Preconditions.checkNotNull;

/**
 * Database based {@link SourceInfoStorage}.
 */

/**
 * HeaderInjector
 *
 * @since 2021-05-10
 */
class DatabaseSourceInfoStorage extends DatabaseHelper implements SourceInfoStorage {
    private static final int DATABASE_VERSION = 1;

    private static final String TABLE = "SourceInfo";
    private static final String COLUMN_ID = "_id";
    private static final String COLUMN_URL = "url";
    private static final String COLUMN_LENGTH = "length";
    private static final String COLUMN_MIME = "mime";
    private static final String[] ALL_COLUMNS = new String[]{COLUMN_ID, COLUMN_URL, COLUMN_LENGTH, COLUMN_MIME};
    private static final String CREATE_SQL =
        "CREATE TABLE " + TABLE + " ("
            + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
            + COLUMN_URL + " TEXT NOT NULL,"
            + COLUMN_MIME + " TEXT,"
            + COLUMN_LENGTH + " INTEGER"
            + ");";

    private final StoreConfig dbConfig = StoreConfig.newDefaultConfig("RdbStoreTest.db");
    private RdbStore db;

    DatabaseSourceInfoStorage(Context context) {
        super(context);
        db = getRdbStore(dbConfig, DATABASE_VERSION, new RdbOpenCallback() {
            @Override
            public void onCreate(RdbStore rdbStore) {
                checkNotNull(rdbStore);
                rdbStore.executeSql(CREATE_SQL);
            }

            @Override
            public void onUpgrade(RdbStore rdbStore, int i, int i1) {
                throw new IllegalStateException("Should not be called. There is no any migration");
            }
        });
    }

    @Override
    public SourceInfo get(String url) {
        checkNotNull(url);
        ResultSet resultSet = null;
        RawRdbPredicates rawRdbPredicates = new RawRdbPredicates(TABLE);
        rawRdbPredicates.setWhereClause(COLUMN_URL + "=?");
        rawRdbPredicates.setWhereArgs(new ArrayList<String>() {
            {
                add(url);
            }
        });
        db.query(rawRdbPredicates, null);
        return null;
    }

    @Override
    public void put(String url, SourceInfo sourceInfo) {
        checkAllNotNull(url, sourceInfo);
        SourceInfo sourceInfoFromDb = get(url);
        boolean isExist = sourceInfoFromDb != null;
        ValuesBucket contentValues = convert(sourceInfo);
        RawRdbPredicates rawRdbPredicates = new RawRdbPredicates(TABLE);
        rawRdbPredicates.setWhereClause(COLUMN_URL + "=?");
        rawRdbPredicates.setWhereArgs(new ArrayList<String>() {
            {
                add(url);
            }
        });
        if (isExist) {
            db.update(contentValues, rawRdbPredicates);
        } else {
            db.insert(TABLE, contentValues);
        }
    }

    @Override
    public void release() {
        db.close();
    }

    private ValuesBucket convert(SourceInfo sourceInfo) {
        ValuesBucket values = new ValuesBucket();
        values.putString(COLUMN_URL, sourceInfo.url);
        values.putLong(COLUMN_LENGTH, sourceInfo.length);
        values.putString(COLUMN_MIME, sourceInfo.mime);
        return values;
    }
}
